﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using FarseerPhysics.Dynamics;
using FarseerPhysics.Dynamics.Contacts;
using Microsoft.Xna.Framework;
using SpriteGame.Rendering;

namespace SpriteGame.Entities
{
    class Player:BaseSprite
    {
        public Player()
        {

        }

        int health = 100;
        public int Health
        {
            get {
                if (deltaHealth > 0)
                {
                    health -= deltaHealth;
                }
                return health; 
            }
            set { health = value; }
        }

        int deltaHealth = 0;
        public int DeltaHealth
        {
            get { return deltaHealth; }
            set { deltaHealth = value; }
        }

        bool isDead = false;
        public bool IsDead
        {
            get { return isDead; }
            set { isDead = value; }
        }

        bool isAiming = false;
        public bool IsAiming
        {
            get { return isAiming; }
            set { isAiming = value; }
        }

        float aimRot = 0.0f;
        public float AimRot
        {
            set { aimRot = value; }
        }

        public Player(Scene _parent, BodyType _type, Vector2 _position, float _rotation, String _texture)
            :base(_parent, _type, _position, _rotation, _texture)
        {

        }

        public override void LoadContent(Scene _parent)
        {
            base.LoadContent(_parent);
            body.FixtureList[0].Friction = 0.85f;
            body.FixtureList[0].OnSeparation += OnSeparation;
            body.FixtureList[0].OnCollision += OnCollision;
        }

        public bool OnCollision(Fixture a, Fixture b, Contact c)
        {
            if (a.Body.LinearVelocity.Y < -9 && (b.Body.Position.Y - a.Body.Position.Y) < 0.0f)
            {
                health += (int)a.Body.LinearVelocity.Y;
                return true;
            }

            return true;
        }

        public override void Update(GameTime _time)
        {
            if (health < 1)
            {
                isDead = true;
                texture = "Textures/Player_Dead";
            }
            base.Update(_time);
        }

        public void OnSeparation(Fixture a, Fixture b)
        {
            object collider = b.Body.UserData;
            if (collider != null)
            {
                if (collider is Ball)
                {
                    health -= 20;
                    parent.Particles.CreateExplosion(Position, 50);
                    parent.RemoveEntity((BaseSprite)collider);
                }

                if (collider is BaseSprite)
                {
                    BaseSprite col = (BaseSprite)collider;
                    float objectVelocity = col.Body.LinearVelocity.Length();
                    float fallVelocity = a.Body.LinearVelocity.Y;
                     
                    //float angle = (float)Math.Acos(Vector2.Dot(Position, col.Position));
                    //if (angle < 1)
                    //{
                    //Console.WriteLine(col.Body.LinearVelocity.Length());
                    if (col.Body.Mass > 0.1f && (objectVelocity > 3f || fallVelocity > 3f))
                    {
                        health -= 10;
                    }
                    //}
                }
            }
        }

        public override void Draw(SpriteGame.Rendering.ShapeRenderer _batch)
        {
            base.Draw(_batch);
            if (isAiming)
            {
                _batch.DrawQuad(Position, aimRot, 6.4f, 19.2f, TextureLibrary.GetTexture("Textures/UI/Arrow"));
            }
        }

        public override void Dispose()
        {
            body.FixtureList[0].OnSeparation -= OnSeparation;
            base.Dispose();
        }
    }
}
